$(function(){

    /**
     * 初始化
     */
    function init(){
        bindEvent();
        //表单的JQueryValidater配置验证---jquery.validate插件验证法
        $("#myForm").validate(validateInfo);
    }

    /**
     * 绑定事件
     */
    function bindEvent(){
        var photo_list = []

        $("#myForm .submit").click(function(){
            editUser();
            return false;
        });

        layui.use(['upload', 'element', 'layer'], function(){
            var $ = layui.jquery
                ,upload = layui.upload
            upload.render({
                elem: '#test2'
                ,url: 'uploadPhoto' //此处配置你自己的上传接口即可
                ,multiple: true
                ,acceptMime:'image/*'
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#demo2').append('<img width="45" height="45" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                    });
                }
                ,done: function(res){
                    console.log(res)
                    //上传完毕
                    if (res.code == 0) {
                        photo_list.push(res.data.src)
                        $('input[name="photo_list"]').val(photo_list.join(','))
                    }
                }
            });
        });
    }
    /**
     * 编辑
     */
    function editUser(){
        $("#myForm").ajaxSubmit($.extend(true,{},formOptions,options,myFormOptions));
    }

    /**
     * 提交添加信息的表单配置
     */
    var  myFormOptions={
        url:'updateMember',
        success:successResponse
    };
    function successResponse(json,statusText){
        if(json.errorCode == 0){
            responseTip(json.errorCode,"恭喜您，修改成功！",1500,function(){window.history.go(-1);});
        }else{
            responseTip(json.errorCode,json.errorInfo,1500);
        }
    }
    //表单验证信息
    var validateInfo ={
        rules:{
            id_card_front:{
                // required: true,
                accept: "jpg,jpeg,gif,png"
            },
        },
        messages:{
            id_card_front:{
                // required:"请选择图片",
                accept:"仅支持jpg、jpeg、gif、png格式"
            },
        },
        errorPlacement:function(error,element){
            element.parent().next().append(error);
        }
    };

    init();
});